package com.scitc.pleasurevideo.adapter;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.scitc.pleasurevideo.R;
import com.scitc.pleasurevideo.pojo.PlayMenuTvVo;
import com.scitc.pleasurevideo.x5webview.MyApplication;

import java.util.List;

public class VideoSelectPlayAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<PlayMenuTvVo> items;
    private Context context;
    private Integer position;

    public VideoSelectPlayAdapter(Context context, List<PlayMenuTvVo> items) {
        this.context = context;
        this.items = items;
        this.position = 0;
    }

    public VideoSelectPlayAdapter(Context context, List<PlayMenuTvVo> items, Integer postion) {
        this.context = context;
        this.items = items;
        this.position = postion;
    }

    public void setHistorySchedule(int currentPosition,int duration){
        if(currentPosition<0||currentPosition>duration){
            return;
        }
        if(this.items==null||this.items.size()<1){
            return;
        }
        this.items.get(position).setCurrentPosition(currentPosition);
        this.items.get(position).setDuration(duration);
    }

    public int getCurrentPosition(){
        return this.items.get(position).getCurrentPosition();
    }

    public void addAll(List<PlayMenuTvVo> items) {
        if (items == null || items.size() < 1) {
            return;
        }
        this.items.clear();
        this.items.addAll(items);
        notifyDataSetChanged();
    }

    public void addItem(PlayMenuTvVo playMenuTvPojo) {
        if (playMenuTvPojo == null) {
            return;
        }
        this.items.add(playMenuTvPojo);
        notifyDataSetChanged();
    }

    public int getCount() {
        if (items == null || items.size() < 1) {
            return 0;
        }
        return items.size();
    }

    public void setPosition(Integer position) {
        if (position < 0 || position > items.size()) {
            this.position = 0;
        } else {
            this.position = position;
        }
        notifyDataSetChanged();
    }

    public int getPosition() {
        if (position < 0 || position > items.size()) {
            return 0;
        }
        return position;
    }

    public List<PlayMenuTvVo> getItems() {
        if (items == null || items.size() < 1) {
            return null;
        }
        return items;
    }

    public PlayMenuTvVo getItem(int position) {
        if (position < 0 || position > items.size()) {
            return null;
        }
        return items.get(position);
    }

    @Override
    public int getItemViewType(int position) {
        if (position < 0 || position > items.size()) {
            return -1;
        }
        if(position+1==items.size()){

        }

        if (position == this.position) {
            return 1;
        } else {
            return 0;
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView title;
        ImageView igDownload;
        TextView tvDownloadText;
        RelativeLayout rlBoxItem;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.tv_video_number);
            igDownload = itemView.findViewById(R.id.igDownload);
            tvDownloadText = itemView.findViewById(R.id.tvDownloadText);
            rlBoxItem = itemView.findViewById(R.id.rlBoxItem);
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        View view = layoutInflater.inflate(R.layout.item_info_video, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        PlayMenuTvVo news = items.get(position);
        if (holder instanceof ViewHolder) {
            ViewHolder viewHolder = (ViewHolder) holder;
            viewHolder.title.setText(news.getName());
            if (this.position == position) {
                viewHolder.rlBoxItem.setBackgroundResource(R.drawable.bg_tv_set_menu_play_on);
                viewHolder.title.setTextColor(ContextCompat.getColor(context, R.color.main_color));
            } else {
                viewHolder.rlBoxItem.setBackgroundResource(R.drawable.bg_tv_set_menu_play_off);
                viewHolder.title.setTextColor(ContextCompat.getColor(context, R.color.black));
            }

            if(MyApplication.isShowDownloadUi){
                viewHolder.igDownload.setVisibility(View.VISIBLE);
                viewHolder.title.setGravity(Gravity.LEFT|Gravity.CENTER_VERTICAL);
            }else {
                viewHolder.igDownload.setVisibility(View.GONE);
                viewHolder.title.setGravity(Gravity.CENTER);
            }

            viewHolder.itemView.setOnClickListener(view -> {
                onClickItemListener.onClickPosition(position, items);
                setPosition(position);
            });
        }
    }

    @Override
    public int getItemCount() {
        return items.size();
    }

    private OnClickItemListener onClickItemListener;

    public interface OnClickItemListener {
        void onClickPosition(int position, List<PlayMenuTvVo> items);
    }

    public void setOnClickItemListener(OnClickItemListener onClickItemListener) {
        this.onClickItemListener = onClickItemListener;
    }
}
